// JavaScript Document
	$(function(){
		
		/*页面属于正中间*/
		var dis=$(".smile_photo_pic").css("display");
		if(dis=="none"){
			$(".smile_photo_upload").css("margin-left","250px");	
		}
		
		
		/*文件上传的高亮显示,和点击事件*/
		$(".smile_photo_way_local").bind({
			mouseover:function(){
				$(this).find('span').css("background-position","-150px -100px");
			},
			mouseout:function(){
				$(this).find('span').css("background-position","-125px -100px");
			},
			click:function(){
					
			}
		});
		
		$(".smile_photo_way_pic").bind({
			mouseover:function(){
				$(this).find('span').css("background-position","-150px -300px");
			},
			mouseout:function(){
				$(this).find('span').css("background-position","-125px -300px");
			}
		});
		
		s_mousemove();
	});
	
	//鼠标移动
	function s_mousemove(){
		var boxDiv = document.getElementById('s_box');//外层容器
		var mainDiv = document.getElementById('s_main');//选择层
		var leftUpDiv = document.getElementById('left-up');//坐上角触点
		var leftDiv = document.getElementById('s_left');//左中间触点
		var leftDownDiv = document.getElementById('left-down');//左下角触点
		var upDiv = document.getElementById('s_up');//上中间触点
		var downDiv = document.getElementById('s_down');//下中间触点
		var rightUpDiv = document.getElementById('right-up');//右上角触点
		var rightDiv = document.getElementById('s_right');//右中间触点
		var rightDownDiv = document.getElementById('right-down');//右下角触点
		
		var ifKeyDown = false;//鼠标按下状态
		var contact = "";//当前拖动的触点
	
		$( "#s_main" ).draggable({ containment: 'parent' ,drag: setChoice});
		
		//设置选取区域高亮可见
		function setChoice(){
			var top = mainDiv.offsetTop;
			var right = mainDiv.offsetLeft + mainDiv.offsetWidth;
			var bottom = mainDiv.offsetTop + mainDiv.offsetHeight;
			var left = mainDiv.offsetLeft;
			$(".img2").css({clip:"rect("+top+"px,"+right+"px,"+bottom+"px,"+left+"px)"});
			var img=$(".img2");
			var can=document.getElementById("scanvas");
			
			var ctx=can.getContext("2d");
			ctx.drawImage(img[0],left,top,200,200,0,0,200,200);
			isrc=ctx.canvas.toDataURL('image/png');
			$("#sphoto").attr("src",isrc);
			//preview({"top":top,"right":right,"bottom":bottom,"left":left});
		}
	}
	
	//获取元素的绝对位置
	function getPosition(node){
		var left = node.offsetLeft;
		var top = node.offsetTop;
		current = node.offsetParent; // 取得元素的offsetParent/获取父元素
		// 一直循环直到根元素
		while (current != null) {
		   left += current.offsetLeft;
		   top += current.offsetTop;
		 current = current.offsetParent;
	  }
		return {"left":left,"top":top};
	}
	
	//点击上传
	function setImagePreviews(file){
		var imgObj=document.getElementById("smile_file");
		var imgDiv=document.getElementById("simle_photo_can");
		
		imgDiv.innerHTML = "";
	
		$(".smile_photo_pic").css("display","block");
		$(".smile_photo_upload").css("display","none");
		$(".smile_photo_pic1").css("display","none");
		$(".simle_photo_video").css("display","none");
		$(".smile_photo_pic").css("margin-left","230px");
		$("#s_box").css("display","none");
		$("#spreview").css("display","none");
		
		var fileList = imgObj.files || file.value;   //火狐 || ie
		 
		
			
		imgDiv.innerHTML += "<div style='display:none' > <img id='img" + 1 + "'  /> </div>";

        var imgObjPreview = document.getElementById("img"+1); 
			
			
        if (imgObj.files && imgObj.files[0]) {    //firefox  chrome
            var reader=new FileReader();
			reader.readAsDataURL(file.files[0]);
			reader.onload=function(evt){
				imgDiv.innerHTML+='<img  id="img"'+1+' src='+ evt.target.result
				+' style="float:left; margin:0px 0px 0px 15px;"  width="302px" height="250px"  />';
			}
         }	
		 
	}
	
		/*点击拍照上传*/
		function s_photoupload(){
			
			var videos=document.getElementById("s_video");
			
			$(".simle_photo_video").css("display","block");
			$(".simle_photo_pic").css("display","none");
			$(".smile_photo_upload").css("display","none");
			$(".smile_photo_pic").css("display","none");
			
			var videoObj={"video":true};
			var errBack=function(error){
				console.log("video canvas error:"+error.code);
			};
			navigator.mozGetUserMedia(videoObj,function(stream){
				videos.src=window.URL.createObjectURL(stream);
				videos.play();	
			},errBack);
		}

		/*点击拍照*/		
		function s_takephoto(){

			$(".smile_photo_pic1").css("display","none");
			$(".simle_photo_video").css("display","none");
			$(".smile_photo_pic").css("display","none");
			$(".smile_photo_upload").css("display","none");
			$("#s_box").css("display","block");
			$("#spreview").css("display","block");
			
			var videos=document.getElementById("s_video");
			var canvass=document.getElementById("s_photo_canvas");
			var context=canvass.getContext("2d");
			context.fillStyle="ccc";
			context.fillRect(0,0,400,300);
			context.drawImage(videos,0,0,300,200);
			isrc=context.canvas.toDataURL('image/png');
			img1="<img class='img1' src="+isrc+" />";
			$(".img1").addClass("img1");
			img2="<img class='img2' src="+isrc+" />";
			$("#s_main").before(img1);
			$("#s_main").before(img2);
			/*img="<img class='simg3' src="+isrc+" />";
			$("#spreview").append(img);*/
		}
		
		//拍照上传
		function photoUpload(){
			var imgData=$("#sphoto").attr("src");
			var sname=$("#sp_sname").val();
			var sdec=$("#sp_sdec").val();
			
			$("#sp_sname").val("");
			$("#sp_sdec").val("");
			imgData=imgData.substring(22);
			$.ajax({
				url:"../json/smileAction_photoUpload",
				type:"post",
				dataType:"json",
				data:{
					imgData:imgData,
					"smile.sname":sname,
					"smile.sdec":sdec,
				},
				success:function(data){
					if ( data.result==1 ){
						window.location.href="../smile/smile.jsp";
					}
				}
			},"json");
		}

		